package com.ojoin.trade.t0loan_core.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.common_core.web.service.MerchantUserBaseService;
import com.ojoin.trade.common_core.web.service.TbMerchantInfoBaseService;
import com.ojoin.trade.t0loan_core.web.dao.TbT0LoanFinalApplicationDao;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanTransTotalInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TxLoanTransTotalInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbOutofSinglePosTranscationLog;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanFinalApplication;
import com.ojoin.trade.t0loan_core.web.service.PosTranscationLogBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanFinalBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanLiveBaseService;

/**
 * T+0贷款信息服务实现
 * 
 * @author guoting
 * 
 */
@Service
public class T0LoanFinalBaseServiceImpl extends
		BaseServiceImpl<TbT0LoanFinalApplication> implements
		T0LoanFinalBaseService {

	@Autowired
	private TbT0LoanFinalApplicationDao dao;

	@Autowired
	private T0LoanLiveBaseService liveService;

	@Autowired
	private MerchantUserBaseService userService;

	@Autowired
	private PosTranscationLogBaseService poslogService;
	
	@Autowired
	private TbMerchantInfoBaseService userinfoService;

	/**
	 * 根据九盈用户id查询用户的T+0贷款信息记录(T0000006)
	 */
	@Override
	public TbT0LoanFinalApplication queryTxLoanFinalInfoByJoinUserID(
			int joinuserid) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"根据九盈用户id查询用户的T+0贷款信息记录. joinuserid->" + joinuserid);
		List<TbT0LoanFinalApplication> selectList = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao()).queryTxLoanFinalInfoByJoinUserID(joinuserid);
		if (selectList == null || selectList.size() == 0)
			return null;

		return selectList.toArray(new TbT0LoanFinalApplication[0])[0];
	}

	


	/**
	 * 正常商户列表--查询所有已申请成功T+0贷款的可放贷九盈用户交易金额合计列表记录-(T0000009)
	 */
	@Override
	public List<T0LoanTransTotalInfoVO> queryAllT0LoanJoinUserTransanctionTotalList(
			Pageable page) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"正常商户列表--查询所有已申请成功T+0贷款的可放贷九盈用户交易金额合计列表记录. pageno->"
						+ page.getPageNumber() + ", pagesize->"
						+ page.getPageSize());
		String shortDate = TimeUtils.getShortCurDate();
		String shortTime = TimeUtils.getShortCurTime();

		List<T0LoanTransTotalInfoVO> infolist = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao()).queryAllT0LoanJoinUserTransanctionTotalList(
				shortDate, shortTime, page);

		return infolist;
	}
	
	@Override
	protected BaseDao<TbT0LoanFinalApplication> getBaseDao() {
		return dao;
	}

	/**
	 * 日额度超限商户列表 (T0000030)
	 */
	@Override
	public List<T0LoanTransTotalInfoVO> queryAllOutOfDayJoinUserTransanctionTotalList(
			Pageable page) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"查询日额度超限商户列表. pageno->" + page.getPageNumber() + ", pagesize->"
						+ page.getPageSize());
		String shortDate = TimeUtils.getShortCurDate();
		String shortTime = TimeUtils.getShortCurTime();

		List<T0LoanTransTotalInfoVO> infolist = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao()).queryAllOutOfDayJoinUserTransanctionTotalList(
						shortDate, shortTime, page);

		// setMhtnameAndMobileByInfoList(infolist);

		return infolist;
	}

	// private void setMhtnameAndMobileByInfoList(
	// List<T0LoanTransTotalInfoVO> infolist) {
	// //设置mhtname、mobile
	// if (infolist == null || infolist.size() == 0)
	// return;
	//
	// LogUtils.getT0loanLog().info("设置商户号对应的商户名和手机号. infolistsize->" +
	// infolist.size() + "");
	// HashMap<String, T0LoanTransTotalInfoVO> mhtnoMap = new HashMap<String,
	// T0LoanTransTotalInfoVO>();
	//
	// for(T0LoanTransTotalInfoVO info : infolist) {
	// mhtnoMap.put(info.getMhtno(), info);
	// }
	//
	// List<String> mhtnoList = new ArrayList<String>();
	// mhtnoList.addAll(mhtnoMap.keySet());
	//
	// if (mhtnoList == null || mhtnoList.size() == 0)
	// return;
	//
	// HashMap<String, MerchantUserInfo> mchUserInfoMap =
	// userService.getMchUserInfoMap(mhtnoList.toArray(new
	// String[mhtnoList.size()]));
	// if (mchUserInfoMap == null || mchUserInfoMap.size() == 0 ||
	// mchUserInfoMap.values() == null || mchUserInfoMap.values().size() == 0)
	// return;
	//
	// for(T0LoanTransTotalInfoVO info : infolist) {
	// if (mchUserInfoMap.get(info.getMhtno()) != null) {
	// info.setMhtname(mchUserInfoMap.get(info.getMhtno()).getMchName());
	// info.setMobile(mchUserInfoMap.get(info.getMhtno()).getMobile());
	// }
	// }
	// }

	/**
	 * 单笔额度超限商户列表 (T0000031)
	 */
	@Override
	public List<T0LoanTransTotalInfoVO> queryAllOutofSingleJoinUserTransanctionTotalList(
			Pageable page) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"查询单笔额度超限商户列表. pageno->" + page.getPageNumber()
						+ ", pagesize->" + page.getPageSize());
		String shortDate = TimeUtils.getShortCurDate();
		String shortTime = TimeUtils.getShortCurTime();

		List<T0LoanTransTotalInfoVO> loglist = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao())
				.queryAllOutofSingleJoinUserTransanctionTotalList(
						shortDate, shortTime, page);
		if (loglist == null || loglist.size() == 0)
			return null;

		return loglist;
	}

	@Override
	public List<TbOutofSinglePosTranscationLog> queryOutofSinglePosTranscationLogListByMhtno(
			String mhtno, Pageable page) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"查询商户" + mhtno + "单笔额度超限pos交易列表. pageno->"
						+ page.getPageNumber() + ", pagesize->"
						+ page.getPageSize());
		String shortDate = TimeUtils.getShortCurDate();
		String shortTime = TimeUtils.getShortCurTime();

		List<TbOutofSinglePosTranscationLog> loglist = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao()).queryOutofSinglePosTranscationLogListByMhtno(
				mhtno, shortDate, shortTime, page);
		if (loglist == null || loglist.size() == 0)
			return null;

		return loglist;
	}

	//////////////////////////////////////////////
	
	/**
	 * 根据九盈用户id查询用户的T+1贷款信息记录
	 */
	@Override
	public TbT0LoanFinalApplication queryTxLoanFinalInfoByJoinUserIDAndType(
			int joinuserid, String operation_type) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"根据九盈用户id查询用户的" + operation_type + "贷款信息记录. joinuserid->" + joinuserid);
		List<TbT0LoanFinalApplication> selectList = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao()).queryTxLoanFinalInfoByJoinUserIDAndType(joinuserid, operation_type);
		if (selectList == null || selectList.size() == 0)
			return null;

		return selectList.toArray(new TbT0LoanFinalApplication[0])[0];
	}
	
	/**
	 * Tx商户交易金额合计列表记录
	 */
	@Override
	public List<TxLoanTransTotalInfoVO> queryAllTxLoanJoinUserTransanctionTotalList(
			Pageable page, String operation_type) throws BusinessException {
		LogUtils.getT0loanLog().info(
				"Tx商户交易金额合计列表记录. operation_type->"
						+ operation_type + ", pageno->"
						+ page.getPageNumber() + ", pagesize->"
						+ page.getPageSize());
//		String shortDate = TimeUtils.getShortCurDate();
//		String shortTime = TimeUtils.getShortCurTime();

		List<TxLoanTransTotalInfoVO> infolist = ((TbT0LoanFinalApplicationDao) this
				.getBaseDao()).queryAllTxLoanJoinUserTransanctionTotalList(page, operation_type);

		return infolist;
	}




	@Override
	public TbT0LoanFinalApplication queryTxLoanFinalInfoByMhtno(String mhtno) throws BusinessException {
		TbMerchantInfo infovo = userinfoService.queryInfoByMhtno(mhtno);
		if (infovo == null)
			return null;
		
		return this.queryTxLoanFinalInfoByJoinUserID(infovo.getIdOfTbMerchantUser());
	}
	
}
